/**
 * 合同协议维护
 * @author miaoyl
 * @date 2018/02/01
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWDD001', width: 160, title: '协议编号', fixed: 'left', sort: true, search: 'default',searchOpts: {icon: true}}
            , {
                field: 'GWDDACTI', width: 80, title: '是否审核', fixed: 'left',
                filterDatas: app.datas_('crm_gwcd_gwcdacti'), search: 'default',
                searchOpts: {type: 'select', default: ''}
            },
            {field: 'GWDD003_NAME', width: 160, title: '客户类型', sort: true}
            , {
                field: 'GWAKL003', width: 160, title: '渠道商', sort: true,
                search: 'default', searchOpts: {icon: true}
            }
            /* , {field: 'GWDD013', width: 160, title: '标准结算价上浮%'}*/
            /*, {field: 'GWDD014', width: 160, title: '年度指标金额'}
             , {field: 'GWDD015', width: 160, title: '年返最高指标%'}*/
            , {field: 'GWDD005', width: 160, title: '生效日期', sort: true}
            , {field: 'GWDD006', width: 160, title: '失效日期', sort: true}
            , {field: 'GWDD004', width: 160, title: '申请日期', sort: true}
            , {field: 'GWDD007_NAME', width: 160, title: '申请人员', sort: true}
            , {field: 'GWDD008_NAME', width: 160, title: '申请部门', sort: true}
            , {field: 'GWDD011', width: 160, title: '备注'},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwdc005setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwdc005SettingAction,
        moduleName = '合同协议'
    ;

    var Gwdc005 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwdc005',
                _searchs: [{
                    id: 'gwdc005', tableId: 'gwdc005_table', datas: _searchsArr.pushArray([
                        {
                            field: 'GWAK010',
                            title: '客户渠道',
                            search: 'default',
                            searchOpts: {icon: true},
                            event: 'openGwam001'
                        }
                        , {
                            field: 'GWAK011',
                            title: '客户等级',
                            search: 'default',
                            searchOpts: {icon: true},
                            event: 'openGwdc001'
                        },
                        {
                            field: 'GWAK023',
                            title: '省',
                            search: 'default',
                            searchOpts: {icon: true},
                            event: 'openGwdc001'
                        }
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwdc005_edit', tpl: 'gwdc005_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            /*var sort = {"field":"GWDD004","sort":"desc"};
             table_param['_SORT'] = JSON.stringify(sort);*/
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwdc005_table'),
                    url: g_ctxPath + 'crm/gwdc005/getGwddlist',
                    exportsUrl: g_ctxPath + 'crm/gwdc005/exportsData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    id: 'gwdc005_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_param,
                });

                layui.laytpl(app.getByid(doc, 'script-gwdc005setting-action').innerHTML).render({}, function (string) {
                    gwdc005SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwdc005_table)', function (obj) {

                    layer.tips(gwdc005SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if (obj.data.GWDDACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的合同协议单才可以编辑！");
                                            return;
                                        }
                                        self._openTplFull({
                                            tag: 'gwdc005_edit',
                                            moduleName: moduleName,
                                            tagData: {obj: obj},
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if (obj.data.GWDDACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的合同协议单才可以删除！");
                                            return;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwdc005_table)', function (obj) {
                    var isCheck = false;
                    if (obj.data.GWDDACTI != 'N') {
                        isCheck = true;
                    }
                    self._openTplFull({
                        tag: 'gwdc005_edit',
                        moduleName: moduleName,
                        tagData: {
                            obj: obj,
                            isCheck: isCheck
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwdc005_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwdc005_edit',
                                moduleName: moduleName,
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            var checkStatus = table.checkStatus('gwdc005_table');
                            var flag = true;
                            if (checkStatus.data.length > 0) {
                                var _xdata = [];
                                checkStatus.data.forEach(function (v) {
                                    var _temp = app.fields_('gwdd_t');
                                    if (v.GWDDACTI != 'N') {
                                        app.showWarning("只有状态为N：未审核的促销活动单才可以删除！");
                                        flag = false;
                                        return;
                                    } else {
                                        _temp.GWDDENT = v.GWDDENT;
                                        _temp.GWDD001 = v.GWDD001;
                                        _xdata.push(_temp);
                                    }
                                });
                                if (flag) {
                                    app.confirm({
                                        msg: '是否删除选择的数据？',
                                        ok: function () {
                                            self.delDataBatch(_xdata);
                                        }
                                    });
                                }
                            }

                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwdc005_table', where: table_param});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwdc005/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function (_xdata) {
            app.ajax({
                xurl: 'crm/gwdc005/delDataBatch',
                xdata: _xdata,
                loading: true,
                toJsonStr: true,
                okmsg: moduleName + '信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc005_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('gwdd_t');
                    if (v.GWDDACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的返利协议才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWDDENT = v.GWDDENT;
                        _temp.GWDD001 = v.GWDD001;
                        _temp.GWDDACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwdc005/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: moduleName + '审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc005_table');
            if (checkStatus.data.length > 0) {
                var myDate = new Date();
                var year = myDate.getFullYear();
                var month = myDate.getMonth() + 1;
                var strDate = myDate.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var time = year + '-' + month + '-' + strDate;
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('gwdd_t');
                    if (v.GWDDACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的返利协议才可以取消审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWDDENT = v.GWDDENT;
                        _temp.GWDD001 = v.GWDD001;
                        _temp.GWDDACTI = "N";
                        _xdata.push(_temp);
                    }
                    /*else if(!(v.GWDD005 > time)){
                     app.showWarning("只有生效日期大于当前时间才可以取消审核！");
                     flag = false;
                     return;
                     } */
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否取消审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwdc005/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: moduleName + '取消审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwdc005',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWAKL003') {//渠道商开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'cq_gwak001_4',
                                            columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAKL003 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择渠道商',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWAK010') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwae001',
                                            columnList: [{label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAK010 = data.GWAE001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户渠道',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWAK011') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwaq001',
                                            columnList: [{label: '客户等级编号', prop: 'GWAQ001', width: '150'},
                                                {label: '说明', prop: 'GWAQL003', width: '150'},
                                                {label: '助记码', prop: 'GWAQL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAK011 = data.GWAQ001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户等级',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWAK023') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwad002',
                                            columnList: [{label: '区域编号', prop: 'GWAD002', width: '150'},
                                                {label: '区域名称', prop: 'GWADL004', width: '150'},
                                                {label: '助记码', prop: 'GWADL005', width: '150'}
                                            ],
                                            searchParam:{
                                                GWAD001: 4
                                            }
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAK023 = data.GWAD002;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择省',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWDD001') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [{label: '合同协议编号', prop: 'GWDD001', width: '150'},
                                                {label: '日期', prop: 'GWDD004', width: '150'},
                                                {label: '申请人员', prop: 'GWDD007_NAME', width: '150'},
                                                {label: '状态码', prop: 'GWDDACTI_NAME', width: '150'}
                                            ],
                                            url: 'crm/gwdc005/getGwddocno'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDD001 = data.GWDD001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择合同协议编号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwdc005();

});
